package com.scoy.honeymei.activity.me;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.lzy.okgo.model.HttpParams;
import com.scoy.honeymei.R;
import com.scoy.honeymei.adapter.CouponListAdapter;
import com.scoy.honeymei.adapter.MoneyAdapter;
import com.scoy.honeymei.base.BaseActivity;
import com.scoy.honeymei.bean.DrawPageBean;
import com.scoy.honeymei.bean.InComeBean;
import com.scoy.honeymei.http.HpCallback;
import com.scoy.honeymei.http.HpGo;
import com.scoy.honeymei.url.MeConstant;
import com.scoy.honeymei.url.MyUrl;
import com.scoy.honeymei.utils.MyUtil;
import com.scoy.honeymei.utils.SPHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ClassName: WithdrawActivity$
 * @Description: 提现界面
 * @Author: GaoZhan
 * @CreateDate: 2019/9/4/0004$ 下午 16:39$
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/9/4/0004$ 下午 16:39$
 * @UpdateRemark: 更新内容
 */
public class WithdrawActivity extends BaseActivity {

    @BindView(R.id.back_iv)
    ImageView backIv;
    @BindView(R.id.title_tv)
    TextView titleTv;
    @BindView(R.id.sign_tv)
    TextView signTv;
    @BindView(R.id.title_llt)
    LinearLayout titleLlt;
    @BindView(R.id.wda_cannum_tv)
    TextView wdaCannumTv;
    @BindView(R.id.wda_rv)
    RecyclerView wdaRv;
    @BindView(R.id.wda_num_et)
    EditText wdaNumEt;
    @BindView(R.id.wda_btn_tv)
    TextView wdaBtnTv;
    private WithdrawActivity mContext;
    private MoneyAdapter moneyAdapter;
    private ArrayList<DrawPageBean.MoneyauthBean> datalist;
    private String withdrawPrice="null";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_withdraw);
        ButterKnife.bind(this);
        mContext = WithdrawActivity.this;

        initNormal();

        httpdraw();
    }

    @Override
    public void initNormal() {
        titleTv.setText(R.string.withdraw);
        MyUtil.setStatusBar(mContext, getWindow(), true, R.color.colorWhite);
        titleLlt.setPadding(0, MyUtil.getStatusBarHeight(mContext), 0, 0);
        datalist = new ArrayList<>();

        initRV();

        wdaNumEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(wdaNumEt.getText())) {
                    wdaBtnTv.setEnabled(false);
                    wdaBtnTv.setBackgroundResource(R.drawable.shape_gray_all90);
                } else {
                    wdaBtnTv.setEnabled(true);
                    wdaBtnTv.setBackgroundResource(R.drawable.shape_blue_all90);
                }

            }
        });


    }

    private void initRV() {
        moneyAdapter = new MoneyAdapter(mContext, datalist);
        wdaRv.setLayoutManager(new GridLayoutManager(mContext, 4, RecyclerView.VERTICAL, false));
        wdaRv.setAdapter(moneyAdapter);
        moneyAdapter.setOnOneClick(new MoneyAdapter.OnOneClick() {
            @Override
            public void oneClick(int pos) {
                DrawPageBean.MoneyauthBean moneyauthBean = datalist.get(pos);
                for (DrawPageBean.MoneyauthBean ibean : datalist) {
                    ibean.setCheck(0);
                }
                moneyauthBean.setCheck(1);
                withdrawPrice = moneyauthBean.getAuth();
                moneyAdapter.notifyDataSetChanged();
            }
        });

    }

    @OnClick({R.id.back_iv, R.id.wda_btn_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_iv:
                finish();
                break;
            case R.id.wda_btn_tv:
                if("null".equals(withdrawPrice)){
                    MyUtil.mytoast(mContext,"请选择提现金额");
                    return;
                }

                httpdrawnow();
                break;
        }
    }

    private void httpdraw() {
        HttpParams params = new HttpParams();
        params.put("userid", SPHelper.getString(MeConstant.UID, ""));
        HpGo.newInstance().HttpGo(this, MyUrl.WITHDRAW_PAGE, params, new HpCallback() {
            @Override
            public void onSuccess(String o) {
                DrawPageBean drawPageBean = new Gson().fromJson(o, DrawPageBean.class);
                String money = drawPageBean.getMyaccount().getMoney();
                wdaCannumTv.setText(money);
                List<DrawPageBean.MoneyauthBean> moneyauth = drawPageBean.getMoneyauth();
                datalist.addAll(moneyauth);
                moneyAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailed(String code, String msg, String o) {
                MyUtil.mytoast(mContext,  msg);
            }

            @Override
            public void onError(int code, String errorMsg) {
                MyUtil.mytoast(mContext, errorMsg);
            }
        });

    }

    private void httpdrawnow() {
        HttpParams params = new HttpParams();
        params.put("userid", SPHelper.getString(MeConstant.UID, ""));
        params.put("money", withdrawPrice);
        params.put("account", wdaNumEt.getText().toString());
        HpGo.newInstance().HttpGo(this, MyUrl.WITHDRAW_NOW, params, new HpCallback() {
            @Override
            public void onSuccess(String o) {
                MyUtil.mytoast(mContext, o);
            }

            @Override
            public void onFailed(String code, String msg, String o) {
                MyUtil.mytoast(mContext,  msg);
            }

            @Override
            public void onError(int code, String errorMsg) {
                MyUtil.mytoast(mContext, errorMsg);
            }
        });

    }

}
